const http = require('../../network/http')
const api = require('../../network/api')
var util = require('../../utils/dateTime.js');
// pages/todayOrder/todayOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeName: '',
    storeId: '',
    orderList: [],
    chooseDay: '2022-10-01',
    toDay: '',
    dataManager: {},
    pageNumber: 1,
    pages: 0,
    dayType: 0,
    fresh: 0,
    arrayType: ["全部类型","微信","美团","余额","抖音","优惠券"],
    selectType: "全部类型",
    showType: false,
  },
  /**
   * 订单列表
   */
  loadincome() {
    var type = "";
    if(this.data.selectType == "微信"){
      type = '1';
    }else if(this.data.selectType == "美团"){
      type = '2';
    }else if(this.data.selectType == "余额"){
      type = '3';
    }else if(this.data.selectType == "抖音"){
      type = '4';
    }else if(this.data.selectType == "优惠券"){
      type = '5';
    }
    http.request({
      showLoading: true,
      url: api.loadInCome,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.chooseDay,
        mode: 0,
        pageNumber: this.data.pageNumber,
        pageSize: 20,
        payType: type,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("订单列表数据返回:");
        console.log(res.data.result);
        var newList = this.data.orderList;
        if(res.data.result.records != null && res.data.result.records.length > 0){
          for(var i=0; i < res.data.result.records.length; i++){
            newList.push(res.data.result.records[i]);
          }
        }
        this.setData({
          'orderList': newList,
          pages: res.data.result.pages,
          // totalPrice: res.data.result[0].monthTotalPrice,
        })
      }
      this.setData({
        fresh: 1,
      })
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  /**
   * 获取总金额
   */
  loadTotalPrice() {
    var type = "";
    if(this.data.selectType == "微信"){
      type = '1';
    }else if(this.data.selectType == "美团"){
      type = '2';
    }else if(this.data.selectType == "余额"){
      type = '3';
    }else if(this.data.selectType == "抖音"){
      type = '4';
    }else if(this.data.selectType == "优惠券"){
      type = '5';
    }
    http.request({
      showLoading: true,
      url: api.getTotalPrice,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.chooseDay,
        mode: 0,
        payType: type,
      },
    }).then(res => {
      if (res.data.code == 200) {
        this.setData({
          dataManager: res.data.result,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  /**
   * 日期选择
   */
  bindDateChange(e) {
    this.setData({
      chooseDay: e.detail.value,
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    this.loadTotalPrice();
    this.loadincome();
  },
  clickroomdetail: function (e) {
    var $data = e.currentTarget.dataset;
    console.log($data.bean);
    var detailId = $data.bean.tradeId;
    wx.navigateTo({
      url: '/pages/detail/detail?detailId=' + detailId + "&storeId=" + this.data.storeId,
    })
  },
  chooseType : function(e){
    var $data = e.currentTarget.dataset;
    console.log($data.bean);
    this.setData({
      showType: false,
      selectType: $data.bean,
      orderList: [],
    })
    this.loadTotalPrice();
    this.loadincome();
  },
  showType : function(e){
    this.setData({
      showType: !this.data.showType,
    })
  },
  closeview : function(e){
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = util.formatTime(new Date());
    var preDate = this.getDateStr(date.substring(0, 10), -1);
    var sendDay;
    if(options.type == 0){
      sendDay = date.substring(0, 10);
    }else{
      sendDay = preDate.substring(0, 10);
    }
    this.setData({
      dayType: options.type,
      storeName: options.storeName,
      storeId: options.storeId,
      chooseDay: sendDay,
      toDay: date.substring(0, 10),
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    this.loadTotalPrice();
    this.loadincome();
  },
  getDateStr: function(today, addDayCount) {
    var date;
    if(today) {
      date = new Date(today);
    }else{
      date = new Date();
    }
    date.setDate(date.getDate() + addDayCount);//获取AddDayCount天后的日期 
      var y = date.getFullYear();
      var m = date.getMonth() + 1;//获取当前月份的日期 
      var d = date.getDate();
      if(m < 10){
        m = '0' + m;
      };
      if(d < 10) {
        d = '0' + d;
      };
      console.log( y + "-" + m + "-" + d)
      return y + "-" + m + "-" + d;
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("onShow---11111111111");
    if(this.data.fresh == 1){
      console.log("onShow---22222222");
      var date = util.formatTime(new Date());
      var preDate = this.getDateStr(date.substring(0, 10), -1);
      var sendDay;
      if(this.data.dayType == 0){
        sendDay = date.substring(0, 10);
      }else{
        sendDay = preDate.substring(0, 10);
      }
      this.setData({
        chooseDay: sendDay,
        toDay: date.substring(0, 10),
        pageNumber: 1,
        pages: 0,
        'orderList': [],
      })
      this.loadTotalPrice();
      this.loadincome();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    this.loadincome();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pages > this.data.pageNumber){
      var newPage = this.data.pageNumber + 1;
      this.setData({
        pageNumber: newPage,
        pages: 0,
      })
      this.loadincome();
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon: "none",
        duration: 2000
      });
    }
    wx.pageScrollTo({
      // scrollTop: 0,
      duration: 300,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})